<template>
  <div>
    <a-modal
      title="新增仓库"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      width="45%"
    >
      <template slot="footer" class="modelBtn">
        <a-button @click="handleCancel">取消</a-button>
        <a-button :loading="loading" @click="handleOk" type="primary"
          >确认</a-button
        >
      </template>
      <a-form-model
        ref="ruleForm"
        :model="formData"
        :rules="formDataRules"
        class="fromData"
      >
        <a-form-model-item
          ref="storeHouseTitle"
          label="仓库名称"
          prop="storeHouseTitle"
        >
          <a-input
            :maxLength="128"
            v-model="formData.storeHouseTitle"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="storeHouseCode"
          label="仓库代码"
          prop="storeHouseCode"
        >
          <a-input
            :maxLength="128"
            v-model="formData.storeHouseCode"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="countryName"
          label="所在国家"
          prop="countryName"
        >
          <a-input
            :maxLength="128"
            v-model="formData.countryName"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="storeContacts"
          label="联系人"
          prop="storeContacts"
        >
          <a-input
            :maxLength="128"
            v-model="formData.storeContacts"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="storeContactNumber"
          label="联系电话"
          prop="storeContactNumber"
        >
          <a-input
            :maxLength="128"
            v-model="formData.storeContactNumber"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="storeHouseAddress"
          label="仓库地址"
          prop="storeHouseAddress"
        >
          <a-input
            :maxLength="128"
            v-model="formData.storeHouseAddress"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item ref="isSelf" label="是否为自营仓" prop="isSelf">
          <div>
            <a-switch :checked="formData.isSelf == 0" @change="changeIsSelf" />
          </div>
        </a-form-model-item>
        <a-form-model-item
          ref="revenuePlan"
          label="收费方案"
          prop="revenuePlan"
        >
          <a-select
            show-search
            option-filter-prop="children"
            v-model="formData.revenuePlan"
            allowClear
            size="small"
            placeholder="收费方案"
          >
            <a-select-option
              :value="item.sysChargingSchemeId"
              v-for="item in revenuePlans"
              :key="item.sysChargingSchemeId"
            >
              {{ item.schemeName }}
            </a-select-option>
          </a-select>
        </a-form-model-item>

        <!-- <a-form-model-item ref="timeZone" label="时区选择" prop="timeZone">
          <a-select
            show-search
            size="small"
            option-filter-prop="children"
            v-model="formData.timeZone"
            allowClear
            placeholder="时区选择"
          >
            <a-select-option
              :value="item.key"
              v-for="item in timeZones"
              :key="item.key"
            >
              {{ item.timeZone }}
            </a-select-option>
          </a-select>
        </a-form-model-item> -->
        <a-form-model-item
          ref="settlementCurrency"
          label="结算币种"
          prop="settlementCurrency"
        >
          <a-select
            show-search
            option-filter-prop="children"
            v-model="formData.settlementCurrency"
            size="small"
            allowClear
            @change="changeCurrency"
            placeholder="结算币种"
          >
            <a-select-option
              :name="item.exchangeCurrency"
              :value="item.sysCurrencyId"
              v-for="item in settlementCurrencys"
              :key="item.sysCurrencyId"
            >
              {{ item.exchangeCurrency }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item ref="business" label="仓库业务" prop="business">
          <a-checkbox
            :checked="this.formData.behalfStock === '0'"
            @change="onChangeBehal"
          >
            代发
          </a-checkbox>
          <a-checkbox
            :checked="this.formData.transport === '0'"
            @change="onchangeTrans"
          >
            转运
          </a-checkbox>
          <a-checkbox
            @change="onchangeRet"
            :checked="this.formData.retreat === '0'"
          >
            退件
          </a-checkbox>
        </a-form-model-item>
        <a-form-model-item label="备注">
          <a-textarea v-model="formData.remarks" :maxLength="256" />
        </a-form-model-item>
        <h4 style="width: 100%">运单发货地址：</h4>
        <a-form-model-item
          ref="waybillCountry"
          label="国家"
          prop="waybillCountry"
        >
          <a-select
            show-search
            option-filter-prop="children"
            v-model="formData.waybillCountryId"
            size="small"
            allowClear
            @change="changeWaybillCountry"
            placeholder="国家"
          >
            <a-select-option
              :value="item.id"
              v-for="item in waybillCountrys"
              :key="item.id"
              :name="item.name"
            >
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item
          ref="waybillProvinceState"
          label="省/州"
          prop="waybillProvinceState"
        >
          <a-select
            allowClear
            size="small"
            show-search
            option-filter-prop="children"
            :filter-option="filterOption"
            @change="handleChangeProvince"
            v-model="formData.waybillProvinceStateId"
            placeholder="请选择州/市"
          >
            <a-select-option
              :value="item.id"
              v-for="item in province"
              :key="item.id"
              :name="item.name"
            >
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item ref="waybillCity" label="城市" prop="waybillCity">
          <a-input
            :maxLength="128"
            v-model="formData.waybillCity"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="waybillPostalCode"
          label="邮编"
          prop="waybillPostalCode"
        >
          <a-input
            :maxLength="128"
            v-model="formData.waybillPostalCode"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="corporateName"
          label="公司名称"
          prop="corporateName"
        >
          <a-input
            :maxLength="128"
            v-model="formData.corporateName"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item
          ref="consignmentAddress"
          label="发货地址"
          prop="consignmentAddress"
        >
          <a-input
            :maxLength="128"
            v-model="formData.consignmentAddress"
            size="small"
          />
        </a-form-model-item>
        <a-form-model-item ref="addressee" label="收件人" prop="addressee">
          <a-input :maxLength="128" v-model="formData.addressee" size="small" />
        </a-form-model-item>
        <a-form-model-item
          ref="contactNumber"
          label="联系电话"
          prop="contactNumber"
        >
          <a-input
            :maxLength="128"
            v-model="formData.contactNumber"
            size="small"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import {
  setWare_addWareHouse,
  setWare_editWareHouse,
  setWare_searchWareHouse,
  logi_getCurrency,
  getRevenPlanList,
} from "@/apis/system/setManage/setWarehouse";
import {
  TransferOut_state,
  TransferOut_Province,
} from "@/apis/clientSide/OrderManage/TransferOut";
0;
export default {
  props: ["getTabDataList"],
  data() {
    return {
      visible: false,
      loading: false,
      handleFlag: "",
      formDataRules: {},
      businessArr: [
        { value: "1", label: "代发" },
        { value: "2", label: "转运" },
        { value: "3", label: "退件" },
      ],
      revenuePlans: [],
      timeZones: [],
      settlementCurrencys: [],
      waybillCountrys: [],
      province: [],
      formData: {
        remarks: null, //备注
        behalfStock: "0",
        retreat: "0",
        transport: "0",
        contactNumber: "", //联系电话
        consignmentAddress: "", //发货地址
        addressee: "", //收件人
        corporateName: "", //公司名称
        countryName: "", //所在国家
        isDisable: 0, //是否禁用
        isSelf: 1, //是否为自营仓
        revenuePlan: undefined, //收费方案
        settlementCurrency: undefined, //结算币种
        settlementCurrencyName: undefined, //结算币种名称
        storeContactNumber: "", //联系电话
        storeContacts: "", //联系人
        storeHouseAddress: "", //仓库地址
        storeHouseCode: "", //仓库代码
        storeHouseTitle: "", //仓库名称
        timeZone: undefined, //时区
        waybillCity: "", //城市
        waybillCityId: "", //城市ID
        waybillCountry: undefined, //运单-国家
        waybillCountryId: undefined, //运单-国家ID
        waybillPostalCode: "", //邮编
        waybillProvinceState: "", //运单-省/州
        waybillProvinceStateId: undefined, //运单-省/州id
      },
    };
  },
  created() {
    //获取币种
    this.getCurrency();
    this.getRevenuePlans();
  },
  mounted() {
    this.$bus.$off("addWareHouse");
    this.$bus.$on("addWareHouse", (flag) => {
      this.getState();
      this.handleFlag = flag;
      this.visible = true;
    });
    this.$bus.$off("editWareHouse");
    this.$bus.$on("editWareHouse", async (row, flag) => {
      this.getState();
      let res = await setWare_searchWareHouse(row.sysStoreHouseId);
      if (res.code == "200") {
        if (res.responseBody.waybillCountryId) {
          this.getProvince(res.responseBody.waybillCountryId);
        }
        this.formData = res.responseBody;
      }
      this.handleFlag = flag;
      this.visible = true;
    });
  },
  methods: {
    //改变仓库业务代发选择框
    onChangeBehal(e) {
      if (e.target.checked) {
        this.formData.behalfStock = "0";
      } else {
        this.formData.behalfStock = "1";
      }
    },
    //改变仓库业务转运选择框
    onchangeTrans(e) {
      if (e.target.checked) {
        this.formData.transport = "0";
      } else {
        this.formData.transport = "1";
      }
    },
    //改变仓库业务退件选择框
    onchangeRet(e) {
      if (e.target.checked) {
        this.formData.retreat = "0";
      } else {
        this.formData.retreat = "1";
      }
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
    //获取收费方案
    async getRevenuePlans() {
      let res = await getRevenPlanList({ size: 10000, current: 1 });
      if (res.code == "200") {
        this.revenuePlans = res.responseBody.records;
      }
    },
    //改变币种选择
    changeCurrency(value, option) {
      this.formData.settlementCurrency = value;
      if (option) {
        this.formData.settlementCurrencyName = option.data.attrs.name;
      }
    },
    //获取币种的接口
    async getCurrency() {
      let res = await logi_getCurrency({ size: 100000, current: 1 });
      if (res.code == "200") {
        this.settlementCurrencys = res.responseBody.records;
      }
    },

    async handleOk() {
      this.loading = true;
      if (this.handleFlag == "add") {
        let res = await setWare_addWareHouse(this.formData);
        if (res.code == "10000") {
          this.getTabDataList();
          this.loading = false;
          this.visible = false;
        } else {
          this.loading = false;
          return false;
        }
      } else if (this.handleFlag == "edit") {
        let res = await setWare_editWareHouse(
          this.formData.sysStoreHouseId,
          this.formData
        );
        if (res.code == "10000") {
          this.getTabDataList();
          this.visible = false;
          this.loading = false;
        } else {
          this.loading = false;
          return false;
        }
      }
    },
    handleCancel() {
      this.formData = {
        remarks: null,
        behalfStock: "0",
        retreat: "0",
        transport: "0",
        contactNumber: "", //联系电话
        consignmentAddress: "", //发货地址
        addressee: "", //收件人
        corporateName: "", //公司名称
        countryName: "", //所在国家
        isDisable: 0, //是否禁用
        isSelf: 1, //是否为自营仓
        revenuePlan: undefined, //收费方案
        settlementCurrency: undefined, //结算币种
        settlementCurrencyName: undefined, //结算币种名称
        storeContactNumber: "", //联系电话
        storeContacts: "", //联系人
        storeHouseAddress: "", //仓库地址
        storeHouseCode: "", //仓库代码
        storeHouseTitle: "", //仓库名称
        timeZone: undefined, //时区
        business: undefined,
        waybillCity: "", //城市
        waybillCityId: "", //城市ID
        waybillCountry: undefined, //运单-国家
        waybillCountryId: undefined, //运单-国家ID
        waybillPostalCode: "", //邮编
        waybillProvinceState: "", //运单-省/州
        waybillProvinceStateId: undefined, //运单-省/州id
      };
      this.visible = false;
    },
    //获取国家接口
    async getState() {
      let { code, responseBody } = await TransferOut_state();
      if (code === "200") {
        this.waybillCountrys = responseBody;
      }
    },
    changeIsSelf(e) {
      if (e) {
        this.formData.isSelf = 0;
      } else {
        this.formData.isSelf = 1;
      }
    },
    async changeWaybillCountry(value, option) {
      if (option != undefined) {
        this.formData.waybillCountry = option.data.attrs.name;
      } else {
        this.formData.waybillCountry = null;
      }
      if (value != undefined) {
        this.getProvince(value);
      }
    },
    //获取州市
    async getProvince(contry) {
      let { code, responseBody } = await TransferOut_Province(contry);
      if (code === "200") {
        this.province = responseBody;
      }
    },
    // 地区更改
    handleChangeProvince(value, option) {
      if (option != undefined) {
        this.formData.waybillProvinceState = option.data.attrs.name;
      } else {
        this.formData.waybillProvinceState = null;
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.fromData {
  display: flex;
  flex-wrap: wrap;
  width: 95%;
  margin-left: 2.5%;
  justify-content: space-between;
  .ant-form-item {
    display: flex;
    width: 50%;
    .ant-form-item-label {
      width: 120px;
      text-align: right;
    }
    .ant-form-item-control-wrapper {
      width: 70%;
    }
  }
}
/deep/.ant-modal-footer {
  display: flex;
  justify-content: center;
  .ant-btn {
    margin-right: 10px;
  }
}
</style>
